package cn.jilian.entity;

import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.jilian.common.config.SpringBeans;
import cn.jilian.meta.service.MetaService;


@Entity
@Table(name="bt_dict",uniqueConstraints = @UniqueConstraint(name="fieldByMidAndName",columnNames = {"mid", "name" }))
public class MField extends BaseEntity {
	
	public static final int FIELD_DICT_RANK=210309;
	
	public static final int FIELD_ATTR_RANK=210310;
	
	public static final String PICKER_NAME="pickerName";
	
	/**
	 * 
	 * */
	public static final String ENUM_MODEL_EFFECT="enumModelEffect";
	
	
	public static final String DEFAULT_VALUE="defaultValue";
	
	public static final String FORCE_SELCTION="forceSelection";
	
	public static final String ENUMS="enums";
	
	public static final String FORMULA="formula";
	
	public static final String STORECOLUMN="storeColumn";
	
	public static final String STOREFORMAT="storeFormat";
	
	public static final String STOREFORMAT_DOTSPLIT="dotSplit";
	
	public static final String STOREFORMAT_IDREF="idReference";
	
	public static final String STOREFORMAT_JSONSTRING="jsonString";
	
	
	public static final String ELEMENTTYPE="elementType";
	
	public static final String VALUEFIELD="valueField";
	
	public static final String DISPLAYFIELD="displayField";
	
	public static final String DATAFORMAT="dataFormat";
	
	public static final String MINLENGTH="validation.minLength";
	
	public static final String MAXLENGTH="validation.maxLength";
	
	public static final String MAX_VALUE="validation.maxValue";
	
	public static final String MIN_VALUE="validation.minValue";
	
	public static final String REGEX="validation.regex";
	
	public static final String REGEXTEXT="validation.regexText";
	
	public static final String VTYPE="validation.vtype";
	
	public static final String VTYPETEXT="validation.vtypeText";
	/**
	 * 步长
	 * */
	public static final String STEP="step";
	
	/**
	 * 表示取值有限集合
	 * */
	public static final String INCLUSION="validation.inclusion";
	
	/**
	 * 表示取值排除项
	 * */
	public static final String EXCLUSION="validation.exclusion";
	
	/**
	 * 计量单位
	 * */
	public static final String UNIT="unit";
	
	/**
	 * 是否必填
	 * */
	public static final String REQUIRED="required";
	
	public static final String REFMODELID="refModelId";
	
	public static final String REFFIELDID="refFieldId";
	
	/**
	 * 互斥字段，当对记录中此字段取值时，其互斥字段不准被使用
	 * */
	public static final String MUTEXT_FIELDS="mutexFields";
	
	/**
	 * 生效表达式，即为当某个字段或其他字段取特定值时配置
	 * */
	public static final String EFFECT_EXPR="effectExpr";
	

	@Column(name="mid")
	private Long fid;
	
	private Integer seq;
	
	private String name;
	
	private String code;
	
	private String text;
	
	@Column(name="dtype")
	private Integer type;
	
	private String mark;
	
	private Integer rank;
	
	private Integer flag=1;
	
	private Date tims;
	
	
	@Transient
	private Map<String,Object> attrs;
	
	
	
	@PostLoad
	@JsonIgnore
	public void convertAttrs(){
		MetaService metaService=SpringBeans.getBean(MetaService.class);
		this.attrs=metaService.getAttrs(getId(), MField.FIELD_ATTR_RANK);
	}
	
	public Boolean  containsAttr(String key) {
		if(attrs==null)return Boolean.FALSE;
		return attrs.containsKey(key);
	}
	public void postUpdate() {
		this.setTims(new Date());
	}

	public Long getFid() {
		return fid;
	}

	public void setFid(Long fid) {
		this.fid = fid;
	}

	public Integer getSeq() {
		return seq;
	}

	public void setSeq(Integer seq) {
		this.seq = seq;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public Integer getRank() {
		return rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public Date getTims() {
		return tims;
	}

	public void setTims(Date tims) {
		this.tims = tims;
	}

	public Map<String, Object> getAttrs() {
		return attrs;
	}
	
	public Object getAttr(String key) {
		if(attrs==null)return null;
		return attrs.get(key);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getAttr(String key,T defaultValue) {
		Object v=getAttr(key);
		if(v==null) {
			v=defaultValue;
		}
		return (T) v;
	}

	public void setAttrs(Map<String, Object> attrs) {
		this.attrs = attrs;
	}
	
	
	
}
